package com.portal.controller;

import com.portal.common.Result;
import com.portal.entity.Feedback;
import com.portal.service.FeedbackService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 反馈Controller
 */
@RestController
@RequestMapping("/feedback")
@CrossOrigin
public class FeedbackController {
    
    private static final Logger log = LoggerFactory.getLogger(FeedbackController.class);
    
    @Value("${file.upload.path}")
    private String uploadPath;
    
    @Value("${file.access.path}")
    private String accessPath;
    
    @Autowired
    private FeedbackService feedbackService;
    
    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public Result<Map<String, String>> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("上传文件不能为空");
        }
        
        try {
            // 生成新的文件名
            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null) {
                return Result.error("文件名不能为空");
            }
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString() + extension;
            
            // 确保上传目录存在
            String uploadDir = uploadPath + "/feedback";
            File dir = new File(uploadDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            
            // 保存文件
            File destFile = new File(uploadDir + "/" + newFileName);
            file.transferTo(destFile);
            
            // 返回文件访问路径
            Map<String, String> result = new HashMap<>();
            result.put("url", accessPath + "/feedback/" + newFileName);
            result.put("name", originalFilename);
            
            return Result.success(result);
            
        } catch (IOException e) {
            log.error("文件上传失败", e);
            return Result.error("文件上传失败：" + e.getMessage());
        }
    }
    
    /**
     * 提交反馈
     */
    @PostMapping("/submit")
    public Result<Void> submitFeedback(@RequestBody Feedback feedback) {
        // 参数校验
        if (StringUtils.isBlank(feedback.getName())) {
            return Result.error("姓名不能为空");
        }
        if (StringUtils.isBlank(feedback.getPhone())) {
            return Result.error("联系电话不能为空");
        }
        if (StringUtils.isBlank(feedback.getCompany())) {
            return Result.error("公司名称不能为空");
        }
        if (StringUtils.isBlank(feedback.getTitle())) {
            return Result.error("反馈标题不能为空");
        }
        if (StringUtils.isBlank(feedback.getContent())) {
            return Result.error("反馈内容不能为空");
        }
        
        try {
            boolean success = feedbackService.submitFeedback(feedback);
            return success ? Result.success(null) : Result.error("提交失败");
        } catch (Exception e) {
            log.error("提交反馈失败", e);
            return Result.error("提交失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取反馈类型列表
     */
    @GetMapping("/types")
    public Result<List<String>> getFeedbackTypes() {
        return Result.success(feedbackService.getFeedbackTypes());
    }
} 
